package com.dawnling.datepickerdemo;

import android.app.Dialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private TimePickerView pvTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        initTimePicker();
    }

    public void btn1(View v){
        DatePickerPopWin_Time pickerPopWin = new DatePickerPopWin_Time.Builder(this, new DatePickerPopWin_Time.OnDatePickedListener() {
            @Override
            public void onDatePickCompleted(int year, int month, String dateDesc) {
                textView.setText("" + year + " " + month + " " + dateDesc);
            }
        })
                .minYear(1990)
                .maxYear(2018)
                .dateChose(CurrenTimeUtil.getCurrenTime())
                .build();
        pickerPopWin.showPopWin(this);
    }

    public void btn2(View v){
        DatePickerPopWin_TimeAll pickerPopWin_timeall = new DatePickerPopWin_TimeAll.Builder(this, new DatePickerPopWin_TimeAll.OnDatePickedListener() {
            @Override
            public void onDatePickCompleted(int year, int month, int day, int hours, int minutes, String dateDesc) {
                textView.setText(dateDesc);
            }
        })
                .minYear(2007) //min year in loop
                .maxYear(2028) // max year in loop
                .dateChose(CurrenTimeUtil.getCurrenTime()) // date chose when init popwindow
                .build();
        pickerPopWin_timeall.showPopWin(this);
    }

    public void btn3(View v){
        DatePickerPopWin_Double pickerPopWin = new DatePickerPopWin_Double.Builder(this, new DatePickerPopWin_Double.OnDatePickedListener() {
            @Override
            public void onDatePickCompleted(int year, int month, int day, String dateDesc) {
                //滚动日历上返回数据
            }

            @Override
            public void onDatePickCompleted1(int year, int month, int day, String dateDesc){
                //滚动日历下返回数据
            }
        })
                .minYear(2000) //min year in loop
                .maxYear(2028) // max year in loop
                .dateChose(CurrenTimeUtil.getCurrenTime()) // date chose when init popwindow
                .build();
        pickerPopWin.showPopWin(this);
    }

    public void btn4(View v){
        pvTime.show(v);//弹出时间选择器，传递参数过去，回调的时候则可以绑定此view
    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();

        //正确设置方式 原因：注意事项有说明
        selectedDate.set(2008,7,8);
        startDate.set(2000,0,1);
        endDate.set(2050,11,31);
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Toast.makeText(MainActivity.this, getTime(date), Toast.LENGTH_SHORT).show();
                Log.i("pvTime", "onTimeSelect");
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .setCancelText("Cancel")//取消按钮文字
                .setSubmitText("Sure")//确认按钮文字
                .setTitleSize(20)//标题文字大小
                .setTitleText("Title")//标题文字
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间
                .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .build();
        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.1f);
            }
        }
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
}
